﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfRcp.Core.Views.Presenters;
using Microsoft.Practices.Composite.Presentation.Commands;
using WpfRcp.Core.Controllers;
using WpfRcp.Modules.Panels.Properties;
using WpfRcp.Core.Services;
using WpfRcp.Core.Common.Workbench;
using WpfRcp.Modules.Panels.Views.Controllers;

namespace WpfRcp.Modules.Panels.Views.Presenters
{
    public class FirstTabPresentationModel : PresenterBase<IFirstTabView>
    {
        public DelegateCommand<object> OpenFileCommand { get; set; }
        public DelegateCommand<object> DisplayFileCommand { get; set; }
        public DelegateCommand<object> OpenAndDisplayFileCommand { get; set; }

        private readonly IContainerFacade m_Container;

        private readonly IDialogController m_DialogController;
        private readonly IWorkbenchService m_WorkbenchService;

        private string m_fileName;
        public string FileName
        {
            get { return m_fileName; }
            private set
            {
                m_fileName = value;
                OnPropertyChanged("FileName");
            }
        }

        public FirstTabPresentationModel(IFirstTabView firstTabView, 
            IContainerFacade container,
            IDialogController dialogController, 
            IWorkbenchService workbenchService)
        {
            View = firstTabView;
            View.Model = this;

            m_Container = container;
            m_DialogController = dialogController;
            m_WorkbenchService = workbenchService;

            OpenFileCommand = new DelegateCommand<object>(ExecuteOpenFileCommand);
            DisplayFileCommand = new DelegateCommand<object>(ExecuteDisplayFileCommand);
            OpenAndDisplayFileCommand = new DelegateCommand<object>(ExecuteOpenAndDisplayFileCommand);
        }

        private void ExecuteOpenFileCommand(object arg)
        {
            string[] filterExtensions;
            string[] filterNames;

            filterExtensions = new string[1];
            filterExtensions[0] = WpfRcp.Modules.Panels.Properties.Resources.TextFilesFilterExtension;

            filterNames = new string[1];
            filterNames[0] = WpfRcp.Modules.Panels.Properties.Resources.TextFilesFilterName;

            DialogResult result = m_DialogController.OpenFileDialog(WpfRcp.Modules.Panels.Properties.Resources.OpenFileWindow, filterNames, filterExtensions, false);
            if (result.Ok)
            {
                FileName = result.FileName;
            }
        }

        private void ExecuteDisplayFileCommand(object arg)
        {
            if (string.IsNullOrEmpty(FileName))
            {
                return;
            }

            var fileContentPresentationModel = m_Container.Resolve<FileContentPresentationModel>();
            var processor = m_Container.Resolve<ContentProcessor>();
            var input = processor.GetInput(FileName);
            fileContentPresentationModel.Text = input.ReadToEnd();
            input.Close();

            var workbenchItem = new DocumentItem(WpfRcp.Modules.Panels.Properties.Resources.Displaying + " - " + FileName, fileContentPresentationModel.View);
            m_WorkbenchService.Add(workbenchItem);
        }

        private void ExecuteOpenAndDisplayFileCommand(object arg)
        {
            ExecuteOpenFileCommand(arg);
            ExecuteDisplayFileCommand(arg);
        }
    }
}
